programming4us
           
 
 
SQL Server

SQL Server 2008: Administering Database Objects - Working with Tables (part 3) - Foreign Key Constraints

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
2/8/2011 11:34:14 AM

4. Foreign Key Constraints

You can create a foreign key to enforce a relationship between the data in two tables. Say that you have an Orders table and an OrderDetails table. When a customer places an order, you insert one record into Orders. Then, for each line item, you place a record into OrderDetails. Naturally you would want each line item in OrderDetails to contain the order number, so that you could refer to the order record for any given line item. That order number in OrderDetails is an example of a foreign key. By defining such a relationship explicitly, you enable SQL Server to ensure that each order number in the child table OrderDetails represents a valid order in the parent table Orders.

NOTE

You cannot change a primary key or unique constraint while it is being referenced by a foreign key constraint; you have to remove the foreign key first.

To create a foreign key you can use the ALTER TABLE statement. You can use the NO CHECK option to force the creation of the foreign key, even if the existing data does not meet the foreign key requirements. All data entered after the foreign key is created will then be forced comply with the constraint. Listing 9-15 uses the ALTER TABLE statement to create a foreign key constraint on the OrderID column in the OrderDetails table that references the OrderID column in the Orders table.

You can specify the CASCADE option to apply any updates or deletes made to the Orders table to the OrderDetails table. For example, if an order is deleted from the Orders table, all of the records in the OrderDetails table that reference that OrderID will be deleted as well.

Example 11. Creating a Foreign Key Constraint Between Two Tables
USE AdventureWorks2008
GO

--Drop the tables if they currently exist
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL
DROP TABLE dbo.OrderDetails;

IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL
DROP TABLE dbo.Orders;

--Create the tables
CREATE TABLE Orders
(OrderID int NOT NULL CONSTRAINT PK_ORDERS PRIMARY KEY CLUSTERED,
OrderNumber int NULL CONSTRAINT UQ_ORDER_NUMBER UNIQUE NONCLUSTERED)

CREATE TABLE OrderDetails
(OrderDetailID int IDENTITY (1,1) NOT NULL
CONSTRAINT PK_ORDER_DETAILS PRIMARY KEY CLUSTERED,
OrderID int NOT NULL,
ProductID int NOT NULL)

--Add the foreign key constraint
ALTER TABLE OrderDetails WITH NOCHECK
ADD CONSTRAINT FK_OrderDetails_Orders FOREIGN KEY(OrderID)
REFERENCES Orders (OrderID)
ON UPDATE CASCADE
ON DELETE CASCADE


The script from Listing 11 creates the tables shown in Figure 2 with a one-to-many relationship between the Orders and the OrderDetails tables.

Figure 2. Foreign key relationship between the Orders and OrderDetails tables

You can disable a foreign key when you want to perform certain operations, such as inserts and deletes that the foreign key would cause to fail. Execute the ALTER TABLE statement with the NOCHECK CONSTRAINT keywords followed by the constraint name. Optionally, you can use the ALL keyword to disable all constraints on a table at once. Re-enable the constraints by changing the NOCHECK keyword to CHECK and reissuing the statement. Listing 12 shows the syntax to disable and enable constraints.

NOTE

You can disable only foreign key and check constraints using the NOCHECK keyword. Any other constraints, such as primary key and unique constraints, will still be enabled.

Example 12. Syntax Used to Disable/Enable Constraints
--Disable the FK_OrderDetails_Orders constraint on the OrderDetails table
ALTER TABLE OrderDetails NOCHECK CONSTRAINT FK_OrderDetails_Orders

--Disable the all constraints on the OrderDetails table
ALTER TABLE OrderDetails NOCHECK CONSTRAINT ALL

--Enable the FK_OrderDetails_Orders constraint on the OrderDetails table
ALTER TABLE OrderDetails CHECK CONSTRAINT FK_OrderDetails_Orders

--Enable the all constraints on the OrderDetails table
ALTER TABLE OrderDetails CHECK CONSTRAINT ALL

To remove a foreign key constraint from a table, issue the ALTER TABLE statement with the DROP CONSTRAINT keywords followed by the name of the foreign key constraint, as shown in Listing 13.

Example 13. Code to Remove a Foreign Key Constraint
ALTER TABLE dbo.OrderDetails DROP CONSTRAINT FK_OrderDetails_Orders
Other -----------------
- SQL Server 2008: Administering Database Objects - Working with Tables (part 2) - Primary Key Constraints & Unique Constraints
- SQL Server 2008: Administering Database Objects - Working with Database Snapshots
- Programming with SQL Azure : WCF Data Services (part 3)
- Programming with SQL Azure : WCF Data Services (part 2) - Creating the Client Application
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 4) - EXPLICIT Mode
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 3) - AUTO Mode
- Programming with SQL Azure : WCF Data Services (part 1)
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 2) - Working with Binary Columns
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 1) - RAW Mode
- Programming with SQL Azure : Connecting to SQL Azure (part 4) - Sqlcmd
- Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us